package com.android.test.view;

import java.util.ArrayList;

import com.android.test.R;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AnimTextView extends LinearLayout {

	public static final int ANIM_TYPE_ROTATE = -1;
	public static final int ANIM_TYPE_SCALE = -2;
	public static final int ANIM_TYPE_TRANSLATE = -3;
	
	private int mCount = 3;
	
	private Context mContext;
	
	private ArrayList<CTextView> mTextViewList = new ArrayList<CTextView>();
	
	public AnimTextView(Context context) {
		this(context, null);
	}

	public AnimTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		this.setOrientation(LinearLayout.HORIZONTAL);
	}
	
	public void setDivideCount(int count) {
		if (count < 0) {
			return;
		}
		this.mCount = count;
	}
	
	public void setTextForView(int position, String string){
		if (position < 0 || position >= mCount) {
			return;
		}
		CTextView view = new CTextView(mContext);
		view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		mTextViewList.add(view);
		view.setTextSize(20f);
		view.setText(string);
		view.setTextColor(Color.BLUE);
		this.addView(view, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	}
	
	public CTextView getChildViewAnim(int position) {
		if (position < 0 || position >= mCount) {
			return null;
		}
		return mTextViewList.get(position);
	}

	public class CTextView extends TextView {
		
		public CTextView(Context context) {
			this(context, null);
		}

		public CTextView(Context context, AttributeSet attrs) {
			this(context, attrs, 0);
		}
		
		public CTextView(Context context, AttributeSet attrs, int defStyle) {
			super(context, attrs, defStyle);
			this.setGravity(Gravity.CENTER_HORIZONTAL);
		}

		/**
		 * Set the animation type of TextView.
		 * @param animTypethis.setGravity(Gravity.CENTER_HORIZONTAL);
		 */
		public void setAnimType(int animType) {
			if (animType == ANIM_TYPE_SCALE) {
				float size = this.getPaint().measureText(this.getText().toString());
				float width = (float) (size*1.9);
				Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.scale_anim);
				this.setAnimation(animation);
				this.setWidth((int) width);
				this.setTextColor(Color.rgb(254, 136, 58));

			}else if (animType == ANIM_TYPE_TRANSLATE) {
				Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.translate_anim);
				this.setAnimation(animation);
			}else if (animType == ANIM_TYPE_ROTATE) {
				Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.rotate_anim);
				this.setAnimation(animation);
			}
			this.postInvalidate();
		}
	}
}